﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Xml;
using System.IO;
using FSMtoCode.Types;
using FSMtoCode.Utils;

namespace FSMtoCode
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Main : Window
    {
        public Main()
        {
            InitializeComponent();
        }

        private void clickedExit(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void clickedOpenFile(object sender, RoutedEventArgs e)
        {
            XMLParser procParser = new XMLParser();

            //Create OpenFileDialog
            OpenFileDialog dlg = new OpenFileDialog();                    

            //Display OpenFileDialog 
            Nullable<bool> result = dlg.ShowDialog();

            string sFileName = "Nenhum selecionado";

            //Get selected file
            if (result == true)
            {
                sFileName = dlg.FileName;
            }

            XmlTextReader XMLReader = new XmlTextReader(sFileName);
         
            //Read the first line 
            try
            {
                XMLReader.Read();
                tblFileName.Text = sFileName;
                MessageBox.Show("File read!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            //Create AST reading file XML
            AST ast = procParser.CreateASTbyXML(XMLReader);

            //Send Ast to Code Generation Manipulation           
            CodeGenerator genCode = new CodeGenerator(ast);
            genCode.GenerateClasses();
            
            String myPath = @"C:\FSMtoCode";
            System.Diagnostics.Process prc = new System.Diagnostics.Process();
            prc.StartInfo.FileName = myPath;
            prc.Start();
        }

        private void btnAbout_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Finite State Machine To Code \nTrabalho de Diplomação - Vinícius Gontijo 1/2011");
        }
    }
}